package gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class LineChart extends ApplicationFrame {

	public LineChart(final String title) {
		super(title);
		final CategoryDataset dataset = createDataset();
		final JFreeChart chart = createChart(dataset);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(500, 270));
		final ChartPanel chartPanel1 = new ChartPanel(chart);
		chartPanel1.setPreferredSize(new Dimension(500, 270));

		Label label = new Label("Test label");
		
		Panel controlPanel = new Panel();
		controlPanel.setLayout(new GridLayout(2,2));
		controlPanel.add(chartPanel);
		
		controlPanel.add(label);
		
		controlPanel.add(chartPanel1);
		
		
		Frame frame = new Frame();
		
		frame.setPreferredSize(new Dimension(1000, 540));
		frame.pack();
		frame.add(controlPanel);
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		
		frame.addWindowListener(new WindowAdapter(){
			  public void windowClosing(WindowEvent we){
			    System.exit(0);
			}
		});

	}

	/**
	 * Creates a sample dataset.
	 * 
	 * @return The dataset.
	 */
	private CategoryDataset createDataset() {

		// row keys...
		final String series1 = "First";
		final String series2 = "Second";
		final String series3 = "Third";

		// column keys...
		final String seconds = "sec ";
		// create the dataset...
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		for (int i = 0; i < 8; i++) {
			double number = new Random().nextDouble();
			dataset.addValue(number * 10, series1, seconds + i);
		}

		for (int i = 0; i < 8; i++) {
			double number = new Random().nextDouble();
			dataset.addValue(number * 10, series2, seconds + i);
		}
		for (int i = 0; i < 8; i++) {
			double number = new Random().nextDouble();
			dataset.addValue(number * 10, series3, seconds + i);
		}

		return dataset;

	}

	/**
	 * Creates a sample chart.
	 * 
	 * @param dataset
	 *            a dataset.
	 * 
	 * @return The chart.
	 */
	private JFreeChart createChart(final CategoryDataset dataset) {

		// create the chart...
		final JFreeChart chart = ChartFactory.createLineChart(
				"Line Chart Demo 1", // chart title
				"Time", // domain axis label
				"Signal", // range axis label
				dataset, // data
				PlotOrientation.VERTICAL, // orientation
				true, // include legend
				true, // tooltips
				false // urls
				);

		final CategoryPlot plot = (CategoryPlot) chart.getPlot();

		// customise the range axis...
		final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		rangeAxis.setAutoRangeIncludesZero(true);

		// customise the renderer...
		final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
				.getRenderer();
		// renderer.setDrawShapes(true);

		return chart;
	}

	/**
	 * Starting point for the demonstration application.
	 * 
	 * @param args
	 *            ignored.
	 */
	public static void main(final String[] args) {

		final LineChart demo = new LineChart("Test");
		// demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		// demo.setVisible(true);

	}

}
